import Vue from 'vue';
import Vuex from 'vuex'
import $H from '../common/request.js';
Vue.use(Vuex)
import {
	  get,
	  post,
	  put,
	  remove,
	  qrpost
	} from '../utils/res'

export default new Vuex.Store({
	state:{
		user:false,
		token:false,
		list:[
			{	
				text: '圈子',
				iconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar1.png',
				selectedIconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabbar-active1.png',
				pagePath: "/pages/circle/circle",
				midButton: false,
			},
			{
				text: '广场',
				iconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar2.png',
				selectedIconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar-active2.png',
				pagePath: "/pages/square/square",
				midButton: false,
			},
			{
				text: '机会',
				iconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabbar3.png',
				selectedIconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabbar-active3.png',
				pagePath: "/pages/chance/chance",
				midButton: false,
			},
			{
				text: '消息',
				iconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar4.png',
				selectedIconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar-active4.png',
				pagePath: "/pages/message/message",
				midButton: false,
				count:0
			},
			{
				text: '我的',
				iconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar5.png',
				selectedIconPath: 'https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/tabbar/tabar-active5.png',
				pagePath: "/pages/my/my",
				midButton: false,
			}
		]
	},
	actions:{
		//获取消息数量
		messageCount({state},e){
			get('/userChatList/unreadMessage').then((res)=>{
				this.state.list[3].count = res.data.num
			})
			// this.state.list[3].count ++
		},
		// 登录
		login({ state },user){
			state.user = user
			state.token = user.token
			uni.setStorageSync('user',JSON.stringify(user))
			uni.setStorageSync('token',user.token)
		},
		// 初始化用户登录状态
		initUser({ state }){
			let u = uni.getStorageSync('user')
			if(u){
				u = JSON.parse(u);
				state.user = u
				state.token = u.token
			}
		},
		// 退出登录
		logout({ state }){
			$H.post('/logout',{},{
				token:true
			})
			state.user = false
			state.token = false
			uni.removeStorageSync('user')
			uni.removeStorageSync('token')
		}
	}
})